

**********************************************************************************************************;
**********************************************************************************************************;

libname w2_ein "projectdir/data/raw_pulls/w2_usiris";

proc import datafile="projectdir/data/raw_pulls/w2_usiris/w2_eins.csv"
out=w2_ein_list
dbms=csv
replace;
run;

DATA work.w2_ein_list;
  SET work.w2_ein_list;
  firmid = '0'||ein;
run;



**********************************************************************************************************;
**********************************************************************************************************;

%macro get_ein_estab(year);

  *MU;

  PROC SORT DATA=w2_ein_list; BY ein; RUN;

  libname ssl "~/economic/ssl/&year";

  DATA work.ssl_mu;
    SET ssl.ssl&year.mu;
  RUN;

  PROC SORT DATA=work.ssl_mu; BY ein; RUN;

  DATA work.match_ssl_mu;
    MERGE work.ssl_mu (IN=A) work.w2_ein_list (IN=B);
    BY ein;
    IF A=1 & B=1 THEN _merge=3;
    IF A=1 & B=0 THEN _merge=1;
    IF A=0 & B=1 THEN _merge=2;
    IF _merge=1 THEN DELETE;
    IF _merge=2 THEN DELETE;
    DROP _merge;

    KEEP ein recnum;
  RUN;

  PROC SORT DATA=work.match_ssl_mu; BY recnum; RUN;


  libname lbd "~/economic/lbd/&year";

  DATA work.lbd_estabs_mu;
    SET lbd.lbd&year.c_c201600;
    IF mu="1";
  RUN;

  PROC SORT DATA=work.lbd_estabs_mu; BY recnum; RUN;

  DATA work.match_lbd_mu;
    MERGE work.match_ssl_mu (IN=A) work.lbd_estabs_mu (IN=B);
    BY recnum;
    IF A=1 & B=1 THEN _merge=3;
    IF A=1 & B=0 THEN _merge=1;
    IF A=0 & B=1 THEN _merge=2;
    IF _merge=1 THEN DELETE;
    IF _merge=2 THEN DELETE;
    DROP _merge;

    KEEP pay ein firmid recnum lbdnum;
  RUN;

  PROC SORT DATA=work.match_lbd_mu; BY lbdnum; RUN;

  DATA work.match_lbd_mu_naics;
    MERGE lbd.naics&year._c201600 (IN=A) work.match_lbd_mu (IN=B);
    BY lbdnum;
    IF A=1 & B=1 THEN _merge=3;
    IF A=1 & B=0 THEN _merge=1;
    IF A=0 & B=1 THEN _merge=2;
    IF _merge=1 THEN DELETE;
    IF _merge=2 THEN DELETE;
    DROP _merge;
  RUN;

  PROC EXPORT
    DATA = work.match_lbd_mu_naics
    OUTFILE = "projectdir/data/raw_pulls/w2_ssl_usiris/lbd_w2_ein_mu_&year..dta"
    DBMS = DTA
    REPLACE;
  RUN;



  *SU;

  PROC SORT DATA=w2_ein_list; BY firmid; RUN;

  DATA work.lbd_estabs_su;
    SET lbd.lbd&year.c_c201600;
    IF mu="0";
  RUN;

  PROC SORT DATA=work.lbd_estabs_su; BY firmid; RUN;

  DATA work.match_lbd_su;
    MERGE work.w2_ein_list (IN=A) work.lbd_estabs_su (IN=B);
    BY firmid;
    IF A=1 & B=1 THEN _merge=3;
    IF A=1 & B=0 THEN _merge=1;
    IF A=0 & B=1 THEN _merge=2;
    IF _merge=1 THEN DELETE;
    IF _merge=2 THEN DELETE;
    DROP _merge;

    KEEP pay ein firmid recnum lbdnum;
  RUN;

  PROC SORT DATA=work.match_lbd_su; BY lbdnum; RUN;

  DATA work.match_lbd_su_naics;
    MERGE lbd.naics&year._c201600 (IN=A) work.match_lbd_su (IN=B);
    BY lbdnum;
    IF A=1 & B=1 THEN _merge=3;
    IF A=1 & B=0 THEN _merge=1;
    IF A=0 & B=1 THEN _merge=2;
    IF _merge=1 THEN DELETE;
    IF _merge=2 THEN DELETE;
    DROP _merge;
  RUN;

  PROC EXPORT
    DATA = work.match_lbd_su_naics
    OUTFILE = "projectdir/data/raw_pulls/w2_ssl_usiris/lbd_w2_ein_su_&year..dta"
    DBMS = DTA
    REPLACE;
  RUN;

%mend;


**********************************************************************************************************;
**********************************************************************************************************;

%get_ein_estab(2002);
%get_ein_estab(2003);
%get_ein_estab(2004);
%get_ein_estab(2005);
%get_ein_estab(2006);
%get_ein_estab(2007);
%get_ein_estab(2008);
%get_ein_estab(2009);
%get_ein_estab(2010);
%get_ein_estab(2011);
%get_ein_estab(2012);
%get_ein_estab(2013);
%get_ein_estab(2014);
%get_ein_estab(2015);
%get_ein_estab(2016);


**********************************************************************************************************;
**********************************************************************************************************;